<template>
  <div id="CardList">
    <div class="card_content">
      <!-- 已购买 -->
      <div v-if="tabstatus === 0">
        <div
          v-for="(item,index) in giftcardlist"
          :key="index"
          class="item"
          :style="{'backgroundImage':'url('+ item.cImgName +')',backgroundPosition:'center',backgroundRepeat: 'no-repeat',backgroundSize: '100% 100%'}"
        >
          <div class="title_box">
            {{ item.giftCardName }}
          </div>

          <div
            class="txt_box"
            :style="{color: item.fontColor}"
          >
            <div class="reduce_box">
              <span>{{ item.reduceAmount }}</span>元
            </div>
            <div class="reduce_box_tow">
              <span>卡号: {{ item.code }}</span>
              <span>密码: {{ item.password }}</span>
            </div>
          </div>

          <div class="time_box">
            <div class="time_item">
              有效期至: {{ item.expireEndDate | rawData }}
            </div>
            <div
              class="time_item buttos"
              @click="Examples(item)"
            >
              转赠
            </div>
            <div class="time_item buttos">
              充值
            </div>
          </div>
          <div class="text_box">
            使用说明: {{ item.directionForUse }}
          </div>
        </div>
      </div>
      <!-- 已购买END -->
      <!-- 已使用 -->
      <div v-if="tabstatus !== 0">
        <div class="table_box">
          <el-table
            :data="giftcardlist"
            style="width: 100%"
            :header-cell-style="{ textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }"
          >
            <el-table-column
              label="礼品卡"
              width="150"
            >
              <template slot-scope="scope">
                {{ scope.row.giftCardName }}
              </template>
            </el-table-column>

            <el-table-column
              label="面值"
              width="150"
            >
              <template slot-scope="scope">
                {{ scope.row.reduceAmount }}元
              </template>
            </el-table-column>

            <el-table-column
              label="卡号"
              width="150"
            >
              <template slot-scope="scope">
                {{ scope.row.code }}
              </template>
            </el-table-column>

            <el-table-column
              label="卡密"
              width="150"
            >
              <template slot-scope="scope">
                {{ scope.row.password }}
              </template>
            </el-table-column>

            <el-table-column
              label="使用时间"
              width="240"
            >
              <template slot-scope="scope">
                {{ scope.row.expireEndDate }}
              </template>
            </el-table-column>

            <!-- <el-table-column
              label="操作"
              width="150"
            >
              <template slot-scope="scope">
                <span
                  class="delete"
                  @click="DeleteCart(scope.row)"
                >删除</span>
              </template>
            </el-table-column> -->
          </el-table>
        </div>
      </div>
      <!-- 已使用 -->
    </div>
    <div class="pagination_box">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pagetotal"
        :current-page.sync="pageIndex"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 转赠 -->
    <div
      v-if="showModalStatus"
      class="overDiv"
    >
      <div class="cancelModal">
        <div class="header">
          <div class="title" />
          <div
            class="colse"
            @click="closeModalModal"
          >
            <img
              src="../../assets/img/close.png"
              alt=""
            >
          </div>
        </div>
        <div class="body">
          <el-form
            ref="form"
            :model="form"
            label-width="7rem"
            :rules="rules"
          >
            <el-form-item
              label="手机号"
              prop="phone"
            >
              <el-input 
                v-model="form.phone"
                placeholder="请输入手机号"
                @input="inputchange"
              >
                <span
                  slot="append"
                  class="search"
                  @click="SearchData(form.phone)"
                >
                  查询
                </span>
              </el-input>
              <div
                v-if="Datacode !== 200"
                class="el-form-item__error"
              >
                {{ DataMsg }}
              </div>
            </el-form-item>
            
            <el-form-item
              label="会员名称"
              prop="name"
            >
              <el-input
                v-model="form.name"
                placeholder="请输入会员名称" 
              />
              <div
                v-if="Datacode2"
                class="el-form-item__error"
              >
                {{ DataMsg2 }}
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="footer">
          <div
            class="yes"
            @click="YESModalModal"
          >
            确认转赠
          </div>
          <div
            class="no"
            @click="closeModalModal"
          >
            取消
          </div>
        </div>
      </div>
      <!-- 转赠end -->
    </div>

    <!-- 删除 -->
    <div
      v-if="DeleteModal"
      class="overDiv"
    >
      <div class="cancelModal">
        <div class="header">
          <div class="title" />
          <div
            class="colse"
            @click="closeModalModal"
          >
            <img
              src="../../assets/img/close.png"
              alt=""
            >
          </div>
        </div>
        <div class="body">
          是否删除所选礼品卡?
        </div>
        <div class="footer">
          <div
            class="yes"
            @click="YESDeleteModal"
          >
            确认
          </div>
          <div
            class="no"
            @click="closeModalModal"
          >
            取消
          </div>
        </div>
      </div>
      <!-- 转赠end -->
    </div>
  </div>
</template>

<script>
export default {
  name: "CardList",
  // 过滤器
		filters: {
			// 去掉小时时间
			rawData(val) {
				let newDate = /\d{4}-\d{1,2}-\d{1,2}/g.exec(val)
				return newDate[0]
			},

		},
  props: {
    giftcardlist: {
      type: Array,
      default: () => [],
    },
    pagetotal: {
      type: Number,
      default: 0,
    },
    tabstatus: {
      type: Number,
      default: 0,
    }
  },
  data() {
    // 手机号验证规则
    let checkPhone = (rule, value, callback) => {
      if (!value) {
          return callback(new Error('请输入手机号'));
        } else {
          const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
          if (reg.test(value)) {
            callback();
          } else {
            return callback(new Error('请输入正确的手机号'));
          }
        }
    };
    return {
      pageIndex: 1, // 分页索引
      showModalStatus:false, // 转赠弹出
      DeleteModal: false, // 删除弹出
      DataMsg: '',
      Datacode: 200, 
      Datacode2: false,
      DataMsg2: '',
      giftCardId: '', // kaid
      DeleteCartRowObj: {}, // 删除数据对象
      form: {
        phone: '',
        name: '',
      },
      OtherPartyObj: {},
      rules: {
        phone: [
          {validator: checkPhone, trigger: 'change'}
        ],
        name: [
          { required: false, message: '请输入会员名称', trigger: 'change' },
        ]
      }
    };
  },
  /* HTML DOM加载后马上执行的，如赋值*/
  computed: {},

  /* 观察Vue实例上的数据变动 */
  watch: {},

  /* 模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图 */
  created() {},

  /* 模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作 */
  mounted() {},

  /* methods则必须要有一定的触发条件才能执行 */
  methods: {
    // 删除操作
    DeleteCart(DeleteCart) {
      this.DeleteModal = true
      this.DeleteCartRowObj = DeleteCart
      
    },
    // 删除 
    YESDeleteModal() {
      console.log('删除', this.DeleteCartRowObj)
      this.$emit('DeleteCart', this.tabstatus)
    },
    // 分页获取当前页
    handleCurrentChange(val) {
      console.log("分页获取当前页", val);
      this.pageIndex = val;
      this.$emit('handleCurrentChange', val)
    },
    // 转赠
    Examples(Examples) {
      console.log('转赠', Examples)
      this.giftCardId = Examples.id
      this.Datacode2 = false
      this.DataMsg2 =  ''
       this.form = {
        phone: '',
        name: '',
      }
      this.showModalStatus = true
    },
    // 确认转赠TransferGiftCard
    YESModalModal() {
       this.$refs.form.validate(valid => {
        // 如果验证失败，不发送请求
        if (!valid) {
          return false
        } else {
          if (JSON.stringify(this.OtherPartyObj) == "{}") {
            this.Datacode2 = true
            this.DataMsg2 =  '请输入手机号,并查询对应会员'
          } else {
            let params = {
              token: localStorage.getItem("token"),
              data: {
                giftCardId: this.giftCardId, // 卡id
                memberId: this.OtherPartyObj.id, // 转让人会员id
                phone: this.OtherPartyObj.cPhone, //转让对方手机号
                iShopId: '',
              }
            }
            this.$api.person.TransferGiftCard(params).then(res =>{
              if(res.code === 200 ) {
                this.showModalStatus = false
                this.$message.success(res.message)
              }  else {
                this.$message.error(res.message)
              }
            })
          } 
        }
       })
    },
    // 取消关闭
    closeModalModal() {
      this.showModalStatus = false
      this.DeleteModal = false
    },
    inputchange() {
      console.log('111111')
      this.Datacode = 200
    },
    // 通过手机号查询
    SearchData(SearchData) {
      // validate是异步请求
      this.$refs.form.validate(valid => {
        // 如果验证失败，不发送请求
        if (!valid) {
          return false
        }
        console.log('通过手机号查询', SearchData)
        let params = {
          token: localStorage.getItem("token"),
          data: {
            phone: SearchData,
            iShopId: '',
          }
        }
        this.$api.person.SearchMember(params).then(res =>{
          if(res.code === 200) {
            this.OtherPartyObj = res.data
            if(res.data.cRealName === undefined) {
              this.form.name = '该用户未设置昵称,不影响转赠'
            } else {
              this.form.name = res.data.cRealName
            }
          } else {
            console.log('asasas')
            this.Datacode = res.code
            this.DataMsg =  res.message
          }
        })
      })
    },
  },
};
</script>

<style style lang='scss' scoped>
#CardList {
  height: 61.5rem;
  background: #fff;
  .card_content{
    padding: 0 1.62rem;
    display: flex;
    flex-wrap: wrap;
    .item{
      flex: 1;
      width: 18rem;
      min-width: 18rem;
      max-width: 18rem;
      margin: 1rem 1.325rem;
      height: 11.3rem;
      padding-top: .6rem;
      .title_box{
        display: flex;
        height: 1.5rem;
        width: 4.5rem;
        align-items: center;
        color: #fff;
        padding: .1rem .8rem;
        font-size: .8rem;
        justify-items: center;
        // margin-top: .6rem;
        background: rgba(255,255,255,.5);
        border-radius: 0rem 1rem 1rem 0;
      }
      .txt_box{
        .reduce_box{
          height: 2rem;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 1rem;
          & span{
            font-size: 1.8rem;
            margin-bottom: .8rem;
          }
        }
        .reduce_box_tow{
          display: flex;
          font-size: .8rem;
          height: 3rem;
          flex-direction: column;
          padding: 0 .7rem;
          & span{
            font-size: .8rem;
            display: flex;
            align-items: center;
            height: 1.5rem;
          }
        }
      }
      .time_box{
        display: flex;
        color: rgba(255,255,255,.6);
        padding: 0 .7rem;
        font-size: .8rem;
        align-items: center;
        height: 2rem;
        .buttos{
          color: #fff;
          border-radius: 10%;
          border:1px solid #fff;
          padding: .2rem .8rem;
          margin: 0 0 0 1rem;
          font-size: .8rem;
          cursor: pointer;
        }
        .buttos:hover{
          color: #f0406f;
          background: #fff;
          border:1px solid #fff;
        }
      }
      .text_box{
        background: hsla(0,0%,100%,.5);
        font-size: .8rem;
        color: #fff;
        display: flex;
        height: 2rem;
        align-items: center;
        padding: 0 .7rem;
      }
    }
  }
  .pagination_box {
    padding: 0 2.62rem;
    display: flex;
    justify-content: center;
    height: 3rem;
    align-items: center;
  }
  .delete{
    cursor: pointer;
  }
  .delete:hover{
    color: #f0406f;
  }
  .overDiv {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);

    .cancelModal2 {
      position: absolute;
      height: 31.69rem;
      width: 39rem;
      padding: 1.25rem;
      left: 50%;
      margin-left: -16.5rem;
      top: 50%;
      margin-top: -9.5rem;
      background: rgba(255, 255, 255, 1);
      z-index: 1001;

      .header {
        padding-top: 3.25rem;
        padding-left: 3.38rem;

        .title {
          font-size: 1.13rem;
          color: #333;
        }
      }

      .body {
        display: flex;
        flex-direction: column;
        padding-top: 3.56rem;

        .tips {
          width: 26rem;
          height: 9.19rem;
          font-size: 1rem;
          margin: 2rem auto 2.69rem auto;
          padding-left: 2rem;

          > div {
            line-height: 2rem;
          }

          ul {
            li {
              list-style: disc;
              font-size: 0.88rem;
              font-weight: 400;
              color: rgba(51, 51, 51, 1);
              line-height: 1.38rem;
            }
          }
        }
        .search{
          cursor: pointer;
        }
        .search:hover{
          color: #f0406f;
        }
      }

      .footer {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        text-align: center;
        line-height: 2.56rem;

        .yes {
          width: 9.5rem;
          height: 2.5rem;
          background: rgba(0, 0, 0, 1);
          color: #fff;
          font-size: 1.13rem;
          border: 0.06rem solid #333;
          margin-right: 5.69rem;
          cursor: pointer;

          &:hover {
            opacity: 0.8;
          }
        }

        .no {
          width: 9.5rem;
          height: 2.5rem;
          background: rgba(255, 255, 255, 1);
          color: #333;
          font-size: 1.13rem;
          border: 0.06rem solid #333;
          cursor: pointer;

          &:hover {
            opacity: 0.8;
          }
        }
      }
    }

    .cancelModal {
      position: absolute;
      // height: 19rem;
      width: 33.19rem;
      padding: 1.25rem;
      left: 50%;
      margin-left: -16.5rem;
      top: 50%;
      margin-top: -9.5rem;
      background: rgba(255, 255, 255, 1);
      z-index: 1001;

      .header {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;

        .titles {
          font-size: 1.13rem;
          color: #666666;
          padding: 0.4rem 0 0 0.5rem;
        }

        .colse {
          cursor: pointer;
          width: 1.88rem;
          height: 1.88rem;

          > img {
            width: 100%;
            height: 100%;
          }

          &:hover {
            opacity: 0.8;
          }
        }
      }

      .body {
        padding: 2.5rem 0;
        text-align: center;
        font-size: 1.13rem;
        color: #333;
      }

      .footer {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        text-align: center;
        line-height: 2.56rem;

        .yes {
          width: 8.19rem;
          height: 2.56rem;
          background: rgba(0, 0, 0, 1);
          color: #fff;
          font-size: 1.13rem;
          border: 0.06rem solid #333;
          margin-right: 5.69rem;
          cursor: pointer;

          &:hover {
            opacity: 0.8;
          }
        }

        .no {
          width: 8.19rem;
          height: 2.56rem;
          background: rgba(255, 255, 255, 1);
          color: #333;
          font-size: 1.13rem;
          border: 0.06rem solid #333;
          cursor: pointer;

          &:hover {
            opacity: 0.8;
          }
        }
      }
    }
  }
}
</style>

<style>
.pagination_box
  .el-pagination.is-background
  .el-pager
  li:not(.disabled).active {
  background-color: rgba(17, 17, 17, 1);
  color: #fff;
}
.pagination_box .el-pagination.is-background .el-pager li:hover {
  color: rgba(17, 17, 17, 1);
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-input-group__append{
  cursor: pointer
}
</style>